<template>
  <div class="map-container" @dblclick="handleDblclick">
    <div class="map-chart" ref="chartRef"></div>
  </div>
</template>

<script>
import axios from 'axios'
import { getProvinceMapInfo } from '../utils/map_utils'
import { mapState } from 'vuex'

export default {
  name: 'Map',
  data() {
    return {
      chartInstance: null,
      allData: null,
      mapData: {}, // 索取到的某省份的地图数据
    }
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme() {
      // 主题改变时，销毁当前图表
      this.chartInstance.dispose()
      // 以最新的主题初始化图表
      this.initChart()
      this.screenAdapter()
      this.updateChart()
    }
  },
  created() {
    this.$socket.registerCallBack('mapData', this.getData)
  },
  mounted() {
    this.initChart()
    //this.getData()
    this.$socket.send({
      action: 'getData',
      socketType: 'mapData',
      chartName: 'map',
      value: ''
     })
    window.addEventListener('resize',this.screenAdapter)
    this.screenAdapter()
  },
  destroyed() {
    window.removeEventListener('resize',this.screenAdapter)
    this.$socket.unRegisterCallBack('mapData')
  },
  methods: {
    // 初始化echarts实例对象
    async initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.chartRef, this.theme)
      // 获取中国地图的矢量数据
      const result = await axios.get('http://localhost:8889/static/map/china.json')
      console.log(result)
      this.$echarts.registerMap('china',result.data)
      const initOption = {
        geo: {
          type: 'map',
          map: 'china',
          top: '5%',
          bottom: '5%',
          itemStyle: {
            areaColor: '#2e72bf',
            borderColor: '#333'
          }
        },
        title: {
          text: '┃ 商家分布',
          left: 20,
          top: 20
        },
        legend: {
          left: '5%',
          bottom: '5%',
          orient: 'vertical', // 设置图例为垂直
        }
      }
      this.chartInstance.setOption(initOption)
      this.chartInstance.on('click', async arg => {
        const provinceInfo = getProvinceMapInfo(arg.name)
        console.log(provinceInfo)
        // 获取某省份的地图矢量数据
        if(!this.mapData[provinceInfo.key]) {
          const result = await axios.get('http://localhost:8889' + provinceInfo.path)
          this.mapData[provinceInfo.key] = result.data
          this.$echarts.registerMap(provinceInfo.key,result.data)
        }
        const changeOption = {
          geo: {
            map: provinceInfo.key
          }
        }
        this.chartInstance.setOption(changeOption)
      })
    },

    // 获取图表的数据
    getData(result) {
      //const result = await this.$http.get('map')
      this.allData = result
      this.updateChart()
    },

    updateChart() {
      // 想要地图中显示散点数据，需要给散点图增加一个配置：coordinateSystem:geo
      const seriesArr = this.allData.map(item => {
        return {
          type: 'effectScatter',
          name: item.name,
          data: item.children,
          coordinateSystem: 'geo',
          // 设置涟漪效果
          rippleEffect: {
            scale: 5,
            brushType: 'stroke'
          }
        }
      })
      const legendArr = this.allData.map(item => {
        return item.name
      })
      const dataOption = {
        series: seriesArr,
        legend: {
          data: legendArr
        }
      }
      this.chartInstance.setOption(dataOption)
    },

    // 屏幕适配
    screenAdapter() {
      const titleFontSize = this.$refs.chartRef.offsetWidth / 100 * 3.6
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        legend: {
          itemWidth: titleFontSize / 2,
          itemHeight: titleFontSize / 2,
          itemGap: titleFontSize / 2,
          textStyle: {
            fontSize: titleFontSize / 2
          }
        }
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    },

    // 双击事件
    handleDblclick() {
      const changeOption = {
        geo: {
          map: 'china'
        }
      }
      this.chartInstance.setOption(changeOption)
    }
  },
}
</script>

<style scoped>

</style>